package com.ws.dao;

import com.ws.bean.Note;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface NoteDao {


    //老板查询待审批的请假记录
//    @Select("<script>select note.*, employee.realname from note, employee "
//            + "<where>"
//            + " and note.eid = employee.eid"
//            + "<if test='ids!=null'> and note.eid in "
//            + "<foreach item='id' collection='ids' open='(' separator=',' close=')'>#{id}</foreach>"
//            + "</if>"
//            + "<if test='status==0'> and status=0</if>"
//            + "<if test='status==1'> and status>0</if>"
//            + "</where>"
//            + "limit #{begin}, #{size}"
//            + "</script>")
//    List<Note> findNoteByPage1(@Param("begin") int begin, @Param("size") int size, @Param("ids") List<Integer> ids,@Param("status") int status);

    @Insert("insert into note values(null, #{title}, #{context}, #{startdate}, #{enddate}, #{length}, now(), 0, null, #{eid})")
    void addNote(Note note);


    @Select("<script> " +
            "select count(*) from note where " +
            "<if test='ids==null'> note.eid = #{eid}</if>" +
            "<if test='ids!=null'> note.eid in " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>#{id}</foreach>" +
            "<if test='status==0'> and status = 0</if>" +
            "<if test='status==1'> and status > 0</if>" +
            "</if>" +
            "</script>")
    int lines(@Param("ids") List<Integer> ids,@Param("status") int status,@Param("eid") int eid);


    @Select("<script>" +
            "select note.*, employee.realname from note, employee where note.eid = employee.eid" +
            "<if test='ids==null'> and note.eid = #{eid}</if>" + //ids==null 说明查找的是自己的请假记录
            "<if test='ids!=null'> and note.eid in " + //ids!=null 则执行老板和经理的待审批和已审批
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>#{id}</foreach>" +
            "<if test='status==0'> and status = 0</if>" +  //status==0 则查询老板或经理的未审批
            "<if test='status==1'> and status > 0</if>" +  //status==1 则查询老板或经理的已审批
            "</if>" +
            "limit #{start}, #{size}" +
            "</script>")
    List<Note> findNoteByPage(@Param("start") int page,@Param("size") int size,@Param("ids") List<Integer> ids,@Param("eid") int eid,@Param("status") int status);

    @Update("update note set status = #{status} where nid = #{nid}")
    void updateStatus(@Param("nid") int nid,@Param("status") int status);
}


